/***********************************************************************
 * Dao Interface :  SysPermissionDao.java
 * Author:  Bill Vison
 * Purpose: Defines the Hibernate Implement SysPermissionDaoImpl
 ***********************************************************************/
package org.vsg.security.model.persistence.hibernate;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.dao.PaginationHibernateDaoSupport;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.security.model.domain.ResourceAssociation;
import org.vsg.security.model.domain.SysResource;
import org.vsg.security.model.domain.SysRole;
import org.vsg.security.model.persistence.ResourceAssociationDao;
import org.vsg.security.model.persistence.hibernate.proc.SysPermissionByAllPaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.SysPermissionPaginationProc;


/**
 * @author Bill Vison
 */
public class ResourceAssociationDaoImpl extends PaginationHibernateDaoSupport implements ResourceAssociationDao {

	/**
	 * set the logger for develope code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(ResourceAssociationDaoImpl.class);

	
	/**
	 *
	 * @param sysPermission
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.ResourceAssociationDao#save(org.vsg.security.model.domain.ResourceAssociation)
	 */
	@Override
	public void save(ResourceAssociation sysPermission) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(sysPermission);
	}

	@Override
	public void saveAll(Collection<ResourceAssociation> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		
		getHibernateTemplate().saveOrUpdateAll(all);
		
	}   	
   	
	/**
	 *
	 * @param sysPermission
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.ResourceAssociationDao#update(org.vsg.security.model.domain.ResourceAssociation)
	 */
   	@Override
   	public void update(ResourceAssociation sysPermission) throws DataAccessException {
   		// TODO Auto-generated method stub
		getHibernateTemplate().update(sysPermission);
	}   	


	/**
	 *
	 * @param sysPermission
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.ResourceAssociationDao#delete(org.vsg.security.model.domain.ResourceAssociation)
	 */
	@Override
	public void delete(ResourceAssociation sysPermission) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(sysPermission);
	}

	@Override
	public void deleteAll(Collection<ResourceAssociation> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().deleteAll(all);
	}
   	
   	
	/**
	 *
	 * @param pk , the primary key
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.ResourceAssociationDao#load(java.io.Serializable)
	 */
	@Override
	public ResourceAssociation load(Serializable pk) throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		ResourceAssociation sysPermission = (ResourceAssociation) getHibernateTemplate().load(ResourceAssociation.class, pk);
		
		return sysPermission;	
		// End of user code	
	}
   
   
	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.ResourceAssociationDao#pageSysPermission(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysPermission(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysPermissionPaginationProc proc = new SysPermissionPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code	
	}      	


	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.ResourceAssociationDao#pageSysPermissionByAll(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysPermissionByAll(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysPermissionByAllPaginationProc proc = new SysPermissionByAllPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code		
	}


	/**
	 * @exception DataAccessException
	 */
	@Override
	public Collection<ResourceAssociation> findByAll() throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");

		query.append("sysPermission ");
		query.append("from ").append(ResourceAssociation.class.getName()).append(" as sysPermission ");
		query.append("where sysPermission.flagDeleted = 0 ");
		query.append("order by sysPermission.name");
		
		Collection params = new Vector();
		
		logger.info("query : " + query.toString());

		Iterator<ResourceAssociation> iter = getHibernateTemplate().iterate(query.toString());
		
		Collection<ResourceAssociation> result = new Vector<ResourceAssociation>();
		
		while (iter.hasNext()) {
			result.add( iter.next() );
		}
		
		return result;
		// End of user code			
	}

	@Override
	public Set<ResourceAssociation> findAssociationByRole(SysRole role)
			throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("resourceAssociation ");
		query.append("from ").append(ResourceAssociation.class.getName());
		query.append(" as resourceAssociation ");
		query.append("left join resourceAssociation.role as role ");
		query.append("where role.id = ?");
		
		Collection params = new Vector();
		params.add( role.getId() );
		
		Set<ResourceAssociation> dataList = new HashSet<ResourceAssociation>();
		
		Iterator<ResourceAssociation> resultIter = this.getHibernateTemplate().iterate(query.toString() , params.toArray());
		while (resultIter.hasNext()) {
			ResourceAssociation asso = resultIter.next();
			dataList.add(asso);
		}
		
		return dataList;		

	}

	@Override
	public Set<ResourceAssociation> findAssocation(SysRole role,SysResource resource) throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("resourceAssociation ");
		query.append("from ").append(ResourceAssociation.class.getName());
		query.append(" as resourceAssociation ");
		query.append("left join resourceAssociation.role as role ");
		query.append("left join resourceAssociation.resource as resource ");
		query.append("where role.id = ? and resource.id = ? ");
		
		Collection params = new Vector();
		params.add(role.getId());
		params.add(resource.getId());
		
		Set<ResourceAssociation> dataList = new HashSet<ResourceAssociation>();
		
		Iterator<ResourceAssociation> resultIter = this.getHibernateTemplate().iterate(query.toString() , params.toArray());
		while (resultIter.hasNext()) {
			ResourceAssociation association = resultIter.next();
			dataList.add(association);
		}
		
		return dataList;
	}

	
   
} 